package com.haohan.stock.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.JdkIdGenerator;

import com.haohan.status.CommnonStatus;
import com.haohan.stock.dao.BaseAreaMapper;
import com.haohan.stock.domain.BaseArea;
import com.haohan.stock.vo.MysqlPage;
import com.haohan.util.TimeUtil;

@Service
public class BaseAreaService {

	@Autowired
	private BaseAreaMapper mapper;

	/**
	 * 保存或修改区域信息
	 * 
	 * @param baseArea
	 * @return
	 * @throws Exception
	 */
	public BaseArea save(BaseArea baseArea) throws Exception {
		baseArea.setDr(CommnonStatus.SAVSTATUS);
		baseArea.setTs(TimeUtil.getTimeAsStr());
		if (baseArea.getId() == null || baseArea.getId().isEmpty()) {
			baseArea.setId(new JdkIdGenerator().generateId().toString());
			mapper.insert(baseArea);
		} else {
			mapper.update(baseArea);
		}
		return baseArea;
	}

	public BaseArea get(BaseArea baseArea) throws Exception {
		baseArea = mapper.selectOne(baseArea);
		return baseArea;
	}

	public MysqlPage<BaseArea> getMysqlPage(MysqlPage<BaseArea> mysqlPage) throws Exception {
		long count = mapper.selectCount(mysqlPage);
		mysqlPage.setTotalElements(count);
		mysqlPage.setTotalPages(count / mysqlPage.getPagesize());
		mysqlPage.setOffset((mysqlPage.getPagenumber()-1)*mysqlPage.getPagesize());
		mysqlPage.setData(mapper.selectPage(mysqlPage));
		return mysqlPage;
	}

	public BaseArea delete(BaseArea baseArea) throws Exception {
		baseArea.setDr(CommnonStatus.DELSTATUS);
		baseArea.setTs(TimeUtil.getTimeAsStr());
		mapper.delete(baseArea);
		return baseArea;
	}

	public List<BaseArea> getList(BaseArea baseArea) throws Exception {
		return mapper.selectList(baseArea);
	}

}